package cn.jt.service;



import cn.jt.dao.UserDao;
import cn.jt.pojo.User;
import cn.jt.vo.PageSysResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.UUID;

@Service("roleService")
public class UserServiceImpl implements  UserService{

    @Resource
    private UserDao userDao;

    @Override
    public List<User> findAll() {
        return userDao.selectList(null);
    }

    /**
     * 1.将明文进行加密处理。md5加密/md5Hash/sha1
     * 2.根据加密后的数据查询数据库
     * 3.判断结果是否有效
     * @param user
     * @return
     */
    @Override
    public String login(User user) {
        //1获取原始密码
        String password=user.getPassword();
        //将密码进行加密处理
        String md5Str= DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
        user.setPassword(md5Str);
        QueryWrapper<User> queryWrapper=new QueryWrapper<User>(user);
        String uuid= UUID.randomUUID().toString();
        if (userDao.selectOne(queryWrapper)!=null){
            return uuid;
        }
        return null;
    }

    @Override
    @Transactional
    public void addUser(User user) {
        String password=user.getPassword();
        String md5Str=DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
        user.setPassword(md5Str);
        user.setStatus(true);
        userDao.insert(user);
    }


    public PageSysResult getUserList1(PageSysResult pageSysResult) {
        Long total=(long) userDao.selectCount(null);
        //页数
        Integer pageNum=pageSysResult.getPageNum();
        //条数
        Integer pageSize=pageSysResult.getPageSize();
        Integer startIndex=(pageNum-1)*pageSize;
        List<User> rows = userDao.getUserList(startIndex,pageSize);
        pageSysResult.setRows(rows);
        pageSysResult.setTotal(total);
        return pageSysResult;
    }
    @Override
    public PageSysResult getUserList(PageSysResult pageSysResult){
        QueryWrapper<User> queryWrapper=new QueryWrapper();
        IPage<User> iPage=new Page(pageSysResult.getPageNum(),pageSysResult.getPageSize());
        Boolean flag= StringUtils.hasLength(pageSysResult.getQuery());
        queryWrapper.like(flag, "username", pageSysResult.getQuery());
        iPage = userDao.selectPage(iPage,queryWrapper);
        //long total=iPage.getTotal();
        pageSysResult.setTotal(iPage.getTotal());
        //List<User> rows=iPage.getRecords();
        pageSysResult.setRows(iPage.getRecords());
        return pageSysResult;
    }

    @Override
    @Transactional
    public void deleteById(Integer id) {

        userDao.deleteById(id);
    }


    @Override
    @Transactional
    public void updateUser(User user) {
        userDao.updateById(user);
        //用于存放模拟的对象，防止GC回收，用List做对象引用

    }


    @Override
    public User findById(Integer id) {
        return userDao.selectById(id);
    }

    @Override
    @Transactional//事务控制注解
    public void updateStatus(Integer id, boolean status) {
        User user=new User();
        user.setStatus(status);
        user.setId(id);
        userDao.updateById(user);

    }

    /**利用MD5进行加密
     * @param str  待加密的字符串
     * @return  加密后的字符串
     * @throws NoSuchAlgorithmException  没有这种产生消息摘要的算法
     * @throws UnsupportedEncodingException
     */
//    public String EncoderByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException, NoSuchAlgorithmException, UnsupportedEncodingException {
//        //确定计算方法
//        MessageDigest md5=MessageDigest.getInstance("MD5");
//        BASE64Encoder base64en = new BASE64Encoder();
//        //加密后的字符串
//        String newstr=base64en.encode(md5.digest(str.getBytes("utf-8")));
//
//        return newstr;
//    }



}
